<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
	
	// ------------------------------------------------------------------------

/**
 * Application
 */
class Distribute extends K9_Deploy_Controller {
	/**
	 * Constructor
	 *
	 * @access public
	 * @return void
	 */
	public function __construct() {
		parent::__construct ();
		$this->load->model('deploy/distribute_task_model');
		$this->load->library ( 'pf/img_lib' );
		$this->_check_permit ();
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Default entrance
	 *
	 * @access public
	 * @param
	 *        	int
	 * @return void
	 */
	public function view() {
		$array = $this->uri->uri_to_assoc(4);
		
		$data['cur_os'] = empty($array['os'])?1:$array['os'];
		$page = empty($array['page'])?1:$array['page'];
		$data['os'] = $this->_get_os_name();
		$data['list'] = $this->distribute_task_model->get_tasks($data['cur_os'], $page-1, 20);
		
		//print_r($data['list']);
		//加载分页
		$this->load->library('pagination');
		$config['base_url'] = backend_url($this->language.'/distribute/view/os/'.$data['cur_os'].'/page');
		$config['per_page'] = 20;
		$config['total_rows'] = $this->distribute_task_model->count($data['cur_os']);
		$config['uri_segment'] = '7';
		$config['query_string_segment'] = 'page';
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$this->_template($this->language.'/deploy/distribute', $data);
	}
	public function do_distribute() {
		
		$os = 1;
		if (! isset ( $_FILES ['apk_file'] ) || empty ( $_FILES ['apk_file'] )) {
			$this->_message ( '请提交游戏文件' );
		}
		$apk_file = $_FILES ['apk_file'];
		$extension = $this->_get_type ( $apk_file ['name'] );
		if (! in_array ( $extension, array (
				'apk','zip'
		) )) {
			$this->_message ( '上传文件的格式不正确，请上传apk' );
		}
		
		$apk_file ['size'] = ( int ) $apk_file ['size'];
		
		if (400 < $apk_file ['size'] / (1024 * 1024)) {
			$this->_message ( '文件太大了' );
		}
		$this->img_lib->set_property ( '409600');
		$this->img_lib->item=array('apk','zip');
		
		$apk_path = $this->img_lib->save_file ( 'apk_file', 'apkdistribute/' , FALSE, $apk_file ['name'] );
		//echo $apk_path;
		//system("echo $(pwd)");
		//system("cd pack_server/androidPack;./distribute.sh ../../../k9img/".$apk_path);
		$tag = $this->input->get_post('tag')?$this->input->get_post('tag'):$apk_file ['name'];
		$chcode=$this->input->get_post('chcode');
		$gameid=$this->input->get_post('gameid');
		$version=$this->input->get_post('version');
		
		$data = array('os' => $os, 'tag' => $tag, 'uri' => $apk_path, 'status'=>0, 'date' => time(), 'chcode' => $chcode, 'gameid' => $gameid, 'version' => $version);
		$this->distribute_task_model->add_task($data);
		$this->_message ( '上传成功!', $this->language.'/distribute/view/os/'.$os, TRUE );
	}
	
	public function del($os,$id){
		$this->distribute_task_model->del_task($id);
		$this->_message ( '删除成功!', $this->language.'/distribute/view/os/'.$os, TRUE );
	}
	
	private function _get_type($name = "") {
		if (! $name)
			return "";
	
		$info = pathinfo ( $name );
		return isset ( $info ['extension'] ) ? $info ['extension'] : "";
	}
	private function _get_os_name()
	{
		$oss = $this->config->item('os');
		$osarray = array();
		foreach ($oss as $k => $v) {
			$osarray[$v] = $k;
		}
		return $osarray;
	}
}

/* End of file user.php */
/* Location: ./admin/controllers/user.php */
